
                                                                                                    /**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;   
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room outside, theater, pub, lab, office,second,first;
      
        // create the rooms
        outside = new Room("风尘客栈",new Thing("补充体力",50));
        theater = new Room("大庄赌场",new Thing("砝码",-200));
        pub = new Room("白浮酒肆",new Thing("猴儿酒",20));
        lab = new Room("崇元府" ,new Thing("玉如意",20));
        office = new Room("清远镖局",new Thing("菩提子",30));
        second = new Room("天地当铺",new Thing("青弘剑",-20));
        first = new Room("百晓生",new Thing("询问玉如意",-30));
        // initialise room exits
        outside.setExit("北",theater);
        outside.setExit("南",office);
        outside.setExit("东",second);
        
        theater.setExit("南",outside);
        theater.setExit("东",pub);
        
        pub.setExit("西",theater);
        pub.setExit("南",second);
        
        office.setExit("北",outside);
        office.setExit("东",first);
        
        second.setExit("西",outside);
        second.setExit("北",pub);
        second.setExit("南",first);
        second.setExit("下",outside);
        
        first.setExit("南",lab); 
        
        currentRoom = outside;  // start game outside
    }   
    
    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("恭喜你完成任务，江湖再见！");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("欢迎来到画中江湖");
        System.out.println("画中江湖顾名思义是画中的一个世界");
        System.out.println("你要克服重重困难进入崇元府，取得玉如意");
        System.out.println("画中江湖顾名思义是画中的一个世界");
        System.out.println("危难时刻可以输入帮助，来寻求帮助");
        System.out.println("你在 " + currentRoom.getDescription());
        currentRoom.printExits();
        System.out.println();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("少侠，你说啥");
            return false;
        }

        String commandWord = command.getCommandWord();
        if (commandWord.equals("帮助")) {
            printHelp();
        }
        else if (commandWord.equals("向")) {
            goRoom(command);
        }
        else if (commandWord.equals("停止")) {
            wantToQuit = quit(command);
        }
        else if (commandWord.equals("捡起")){
            pick();
        }
        else if (commandWord.equals("查看")){
            look();
        }
        else if (commandWord.equals("使用")){
            use();
        }
        else if (commandWord.equals("检查")){
            check();
        }

        return wantToQuit;
    } 
    private void pick(){
        Thing thing= currentRoom.getThing();
        if(thing !=null){
            player.pick(thing);
            System.out.println("你拥有了"+ thing.getName() +".");
            currentRoom.removeThing();
        }else {
            System.out.println("没有东西.");
        }
    }
    private void look(){
        Thing thing= currentRoom.getThing();
        player.look(thing);
        if(thing !=null){
            player.look(thing);
            System.out.println("物品：" + thing.getName() +  ", 体力：" + thing.getEnergy() + " 。");
        }
    }
    private void use(){
        Thing thing= currentRoom.getThing();
        if(thing !=null){
          System.out.println("你使用了"+thing.getName());
          player.strength +=thing.getEnergy();
          player.use(thing.getName());
        }
    }
    private void check(){
        Thing thing=currentRoom.getThing();
        System.out.println("体力 "+player.strength());  
        if(player.strength()<70){
            System.out.println("体力不支，必须尽快完成任务");
            System.out.println("或者返回客栈补充体力 ");
            System.out.println("未在体力消耗完之前完成任务，将会任务失败");
            }
        if(player.strength()<0){
            System.out.println("初入江湖，切记十赌九输");
            System.out.println("青山不改，绿水长流 ");
            System.exit(1);//退出游戏
            }
  }

  private void printHelp() 
    {
        System.out.println("初入江湖，且行且小心");
        System.out.println("你可以去找百晓生");
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("往哪跑");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goRoom(direction);
        
        
        if (nextRoom == null) {
            System.out.println("少侠，你去哪");
        }
        else {
            currentRoom = nextRoom;
            System.out.println("你在" + currentRoom.getDescription());
            System.out.println("体力："+ player.strength );
            System.out.print("可去之处:");
            player.move();
            currentRoom.printExits();
        }
        if(currentRoom.getDescription() == "百晓生") {
                    System.out.println("恭喜你，我早已等候多时");
                    System.out.println("玉如意，就在崇元府中");
                    System.out.println("崇元府，在正南方，一切小心！");
                }
        if(currentRoom.getDescription() == "崇元府") {
                    System.out.println("府内装饰富丽堂皇，雕梁画栋");
                    System.out.println("你记得图纸上说玉如意在西厢房");
                    System.out.println("你健步如飞，如入无人之地，到了西厢房");
                    System.out.println("取得玉如意");
                    System.out.println("突然白光一闪，一行文字出现");
                    System.out.println("恭喜你完成任务，江湖再见！");
                }        
    }

    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("想跑？");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}


